﻿using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.BPW
{
    [Description("BPW:应收单整单分摊金额审核时进行分摊"), HotUpdate]
    public class DzReceivable : AbstractOperationServicePlugIn
    {
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            var obj = e.DataEntitys.FirstOrDefault();
            var formId = this.BusinessInfo.GetForm().Id;
            var operationNumber = this.FormOperation.Operation;
            var id = Convert.ToInt32(obj["Id"]);
            if (formId != null && formId == "AR_receivable")
            {
                ReSaveReceivable(operationNumber, id, this.OperationResult);
            }
        }
        /// <summary>
        /// 重新保存应收单
        /// </summary>
        /// <param name="operationNumber"></param>
        /// <param name="id"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ReSaveReceivable(string operationNumber, int id, Kingdee.BOS.Core.DynamicForm.IOperationResult operationResult)
        {
            //创建视图
            IBillView billView = CreateBillView.CreateView(this.Context, "AR_receivable");
            ((IBillViewService)billView).LoadData();
            DynamicFormViewPlugInProxy eventProxy = billView.GetService<DynamicFormViewPlugInProxy>();
            eventProxy.FireOnLoad();
            billView.Model.Load(id);
            decimal fdje = Convert.ToDecimal(billView.Model.DataObject["F_BPW_Amount"]);
            DynamicObjectCollection rows = billView.Model.DataObject["AP_PAYABLEENTRY"] as DynamicObjectCollection;
            decimal yunZongJinE = rows.Sum(x => Convert.ToDecimal(x["FALLAMOUNTFOR_D"]));
            if (operationNumber == "Audit")
            {
                if (fdje > 0)
                {
                    int index = 0;
                    for (int i = 0; i < rows.Count; i++)
                    {
                        decimal yunJiaShuiHeJi = Convert.ToDecimal(rows[i]["FALLAMOUNTFOR_D"]);
                        decimal xinJiaShuiHeJi = yunJiaShuiHeJi + yunJiaShuiHeJi / yunZongJinE * fdje;
                        decimal qty = Convert.ToDecimal(rows[i]["PriceQty"]);
                        decimal price = xinJiaShuiHeJi / qty;
                        billView.Model.SetValue("FTaxPrice", price, i);
                        billView.InvokeFieldUpdateService("FTaxPrice", i);
                        index = i;
                    }
                    decimal xinZongjinE = rows.Sum(s => Convert.ToDecimal(s["FALLAMOUNTFOR_D"]));
                    decimal chayi = xinZongjinE - (yunZongJinE + fdje);
                    if (chayi != 0)
                    {
                        decimal yunJiaShuiHeJi = Convert.ToDecimal(rows[index]["FALLAMOUNTFOR_D"]);
                        if (chayi > 0)
                        {
                            billView.Model.SetValue("FALLAMOUNTFOR_D", yunJiaShuiHeJi - chayi, index);
                        }
                        else
                        {
                            billView.Model.SetValue("FALLAMOUNTFOR_D", yunJiaShuiHeJi + chayi, index);
                        }
                    }
                }
            }
            if (operationNumber == "UnAudit")
            {
                for (int i = 0; i < rows.Count; i++)
                {
                    decimal FTaxPriceLog = Convert.ToDecimal(rows[i]["FTaxPriceLog"]);
                    if (FTaxPriceLog > 0)
                    {
                        billView.Model.SetValue("FTaxPrice", FTaxPriceLog, i);
                        billView.InvokeFieldUpdateService("FTaxPrice", i);
                    }
                }
            }

            //保存单据
            CreateBillView.OnlySaveBill(billView, OperateOption.Create(), this.Context, operationResult);
        }
    }
}
